﻿using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using API;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class VCTestScroll : UIViewController
    {
        static ILog logger = mb.ServiceLocator.Instance.Get<ILog>();

        MyScrollView outScrollView;
        MyScrollView inScrollView;
        UIView topBar1;
        UIView topBar2;

        public VCTestScroll()
        {
            TabBarItem = new UITabBarItem("阅读", null, 0);
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.BackgroundColor = UIColor.Purple;
            logger.Log("VCBook ViewDidLoad");

            outScrollView = new MyScrollView()
            {
                BackgroundColor = UIColor.Gray,
                    MyShouldRecognizeSimultaneously = (v, rg1, rg2) =>
                    {
                        var rg1Pan = rg1 as UIPanGestureRecognizer;

                        return false;
                    }
            };
            outScrollView.SetParent(View);
            outScrollView.MakeConstraints(m =>
            {
                m.Edges.EqualTo(View);
            });

            AddRuleToScrollView(outScrollView);

            topBar1 = new UIView() { BackgroundColor = UIColor.Red };
            topBar1.SetParent(outScrollView);
            topBar1.MakeConstraints(m =>
            {
                m.Width.EqualTo(outScrollView);
                m.Top.EqualTo(outScrollView);
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Height.EqualTo(NSObject.FromObject(100));
            });

            topBar2 = new UIView() { BackgroundColor = UIColor.Green };
            topBar2.SetParent(outScrollView);
            topBar2.MakeConstraints(m =>
            {
                m.Width.EqualTo(outScrollView);
                m.Top.EqualTo(topBar1.Bottom());
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Height.EqualTo(NSObject.FromObject(100));
            });

            inScrollView = new MyScrollView()
            {
                BackgroundColor = UIColor.Blue,
                    MyShouldRecognizeSimultaneously = (v, rg1, rg2) =>
                    {
                        var rg1Pan = rg1 as UIPanGestureRecognizer;

                        return false;
                    }
            };
            inScrollView.SetParent(outScrollView);
            inScrollView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Top.EqualTo(topBar2.Bottom());
                // m.Right.EqualTo(outScrollView.Right());
                // m.Bottom.EqualTo(outScrollView);
                m.Height.EqualTo(NSObject.FromObject(1000));
                m.Width.EqualTo(outScrollView);
            });
            AddRuleToScrollView(inScrollView);
        }

        void AddRuleToScrollView(UIScrollView sv)
        {
            int totalH = 3000;
            int segH = 50;
            int xb = 50;
            int y = 0;
            for (int i = 0; i < 50; i++)
            {
                y = i * segH;
                var tv = new UILabel();
                tv.Frame = new CoreGraphics.CGRect(0, y, xb, 30);
                tv.Text = y.ToString();
                tv.SetParent(sv);
            }
            sv.ContentSize = new CoreGraphics.CGSize(400, y);
        }

    }
}